home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / EventDispatchThread.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  2.4 KB  |  90 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)EventDispatchThread.java    1.26 98/08/11
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. /**
  18.  * EventDispatchThread is a package-private AWT class which takes
  19.  * events off the EventQueue and dispatches them to the appropriate
  20.  * AWT components.
  21.  *
  22.  * @version 1.26 08/11/98
  23.  * @author Tom Ball
  24.  * @author Amy Fowler
  25.  * @author Fred Ecks
  26.  */
  27. class EventDispatchThread extends Thread {
  28.     private EventQueue theQueue;
  29.     private boolean doDispatch = true;
  30.  
  31.     EventDispatchThread(String name, EventQueue queue) {
  32.     super(name);
  33.         theQueue = queue;
  34.     }
  35.  
  36.     public void stopDispatching() {
  37.     // Note: We stop dispatching via a flag rather than using
  38.     // Thread.interrupt() because we can't guarantee that the wait()
  39.     // we interrupt will be EventQueue.getNextEvent()'s.  -fredx 8-11-98
  40.  
  41.         doDispatch = false;
  42.  
  43.     // fix 4122683, 4128923
  44.     // Post an empty event to ensure getNextEvent is unblocked
  45.     theQueue.postEvent(new EmptyEvent());
  46.  
  47.     // wait for the dispatcher to complete
  48.     if (Thread.currentThread() != this) {
  49.         try {
  50.         join();
  51.         } catch(InterruptedException e) {
  52.         }
  53.     }
  54.     }
  55.  
  56.     class EmptyEvent extends AWTEvent implements ActiveEvent {
  57.     public EmptyEvent() {
  58.         super(EventDispatchThread.this,0);
  59.     }
  60.  
  61.     public void dispatch() {}
  62.     }
  63.  
  64.     public void run() {
  65.        while (doDispatch && !isInterrupted()) {
  66.             try {
  67.                 AWTEvent event = theQueue.getNextEvent();
  68.                 theQueue.dispatchEvent(event);
  69.             } catch (ThreadDeath death) {
  70.                 return;
  71.  
  72.         } catch (InterruptedException interruptedException) {
  73.         return; // AppContext.dispose() interrupts all
  74.             // Threads in the AppContext
  75.  
  76.             } catch (Throwable e) {
  77.                 System.err.println(
  78.                     "Exception occurred during event dispatching:");
  79.                 e.printStackTrace();
  80.             }
  81.         }
  82.     }
  83.  
  84.     boolean isDispatching(EventQueue eq) {
  85.     return theQueue.equals(eq);
  86.     }
  87.  
  88.     EventQueue getEventQueue() { return theQueue; }
  89. }
  90.